package com.jcfk.eam.controller.equ;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.domain.dto.base.EamLocationInfoDTO;
import com.jcfk.eam.domain.dto.base.EamRepairOrderDTO;
import com.jcfk.eam.domain.dto.base.EamRepairOrderStatusDTO;
import com.jcfk.eam.domain.dto.base.EamRepairOrderTransmitDTO;
import com.jcfk.eam.domain.dto.main.RepairOrderSummaryDTO;
import com.jcfk.eam.domain.po.base.EamRepairOrder;
import com.jcfk.eam.domain.query.base.EamRepairOrderQuery;
import com.jcfk.eam.enums.AssetKind;
import com.jcfk.eam.enums.RepairOrderRecordOperateType;
import com.jcfk.eam.service.base.EamLocationInfoService;
import com.jcfk.eam.service.main.EamRepairOrderService;
import com.jcfk.eam.service.main.EamRepairOrderTransmitService;
import com.jcfk.eam.service.main.EamWorkOrderRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * 设备维修单
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-08-26
 */
@Api(tags = {"设备故障维修"})
@RestController
@RequestMapping("equrepairorder")
public class EquRepairOrderController {
    @Autowired
    private EamRepairOrderService eamRepairOrderService;

    @Autowired
    private EamWorkOrderRecordService eamWorkOrderRecordService;

    @Autowired
    private EamLocationInfoService eamLocationInfoService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamRepairOrderQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询维修单")
    @PostMapping("/queryByPage")
    public RetPage<EamRepairOrderDTO> queryByPage(@RequestBody ReqPageParam<EamRepairOrderQuery> requestParam) {
        requestParam.getData().setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        DataPage<EamRepairOrderDTO> page = new DataPage<EamRepairOrderDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), EamRepairOrderDTO.class);

        // 设置设备类型查询条件
        if (!StrUtil.isEmpty(requestParam.getData().getLocationId())) {
            EamLocationInfoDTO eamLocationInfoDTO = eamLocationInfoService.get(requestParam.getData().getLocationId());
            if (eamLocationInfoDTO != null) {
                requestParam.getData().setLocationId(eamLocationInfoDTO.getAllId());
            }
        }

        page = eamRepairOrderService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 获取维修管理汇总
     */
    @ApiOperation("获取维修管理汇总")
    @PostMapping("/getRepairOrderSummary")
    public RetResult<RepairOrderSummaryDTO> getRepairOrderSummary(@RequestBody EamRepairOrderQuery repairOrderQuery){
        RepairOrderSummaryDTO repairOrderSummary=eamRepairOrderService.getRepairOrderSummary(repairOrderQuery);
        return RetResult.ok(repairOrderSummary);
    }

    /**
     * 分页查询我的报修单
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamRepairOrderQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询我的报修单")
    @PostMapping("/queryMyReportOrder")
    public RetPage<EamRepairOrderDTO> queryMyReportOrder(@RequestBody ReqPageParam<EamRepairOrderQuery> requestParam) {
        requestParam.getData().setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        DataPage<EamRepairOrderDTO> page = new DataPage<EamRepairOrderDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), EamRepairOrderDTO.class);
        requestParam.getData().setRepairUser(SecurityUtils.getUserId());

        page = eamRepairOrderService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 分页查询我的维修单
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamRepairOrderQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询我的维修单")
    @PostMapping("/queryMyRepairOrder")
    public RetPage<EamRepairOrderDTO> queryMyRepairOrder(@RequestBody ReqPageParam<EamRepairOrderQuery> requestParam) {
        requestParam.getData().setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        DataPage<EamRepairOrderDTO> page = new DataPage<EamRepairOrderDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), EamRepairOrderDTO.class);
        requestParam.getData().setRepairTeamSysUser(SecurityUtils.getUserId());

        page = eamRepairOrderService.queryMyRepairOrder(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 获取我的维修管理汇总
     */
    @ApiOperation("获取我的维修管理汇总")
    @PostMapping("/getMyRepairOrderSummary")
    public RetResult<RepairOrderSummaryDTO> getMyRepairOrderSummary(@RequestBody EamRepairOrderQuery repairOrderQuery){
        RepairOrderSummaryDTO repairOrderSummary=eamRepairOrderService.getMyRepairOrderSummary(repairOrderQuery);

        return RetResult.ok(repairOrderSummary);
    }

    /**
     * 维修单
     * 根据id 查询详情
     */
    @ApiOperation("查询维修单")
    @GetMapping("/get/{id}")
    public RetResult<EamRepairOrderDTO> get(@PathVariable("id") String id) {
        EamRepairOrderDTO item = eamRepairOrderService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理维修单
     */
    @ApiOperation("保存维修单")
    @PostMapping("/save")
    @RequestLogHeader(modular = "维修单管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamRepairOrderDTO eamRepairOrder) {
        // 数据校验
        RetResult ret = eamRepairOrderService.isValid(eamRepairOrder);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        eamRepairOrder.setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());

        if (StrUtil.isEmpty(eamRepairOrder.getId())) {
            // 新增
            eamRepairOrderService.insert(eamRepairOrder);
        } else {
            // 更新
            eamRepairOrderService.update(eamRepairOrder);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除维修单
     */
    @ApiOperation("批量删除维修单")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "维修单管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamRepairOrderDTO eamRepairOrder) {
        return RetResult.ok(eamRepairOrderService.logicDeleteList(eamRepairOrder));
    }


    /**
     * 删除维修单
     */
    @ApiOperation("删除维修单")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "维修单管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamRepairOrderService.deleteItem(id);
        return RetResult.ok();
    }


    /**
     * 修改维修单状态
     */
    @ApiOperation("修改维修单状态")
    @PostMapping("/updateStatus")
    @RequestLogHeader(modular = "维修单管理", businessType = "修改维修单状态")
    public RetResult<?> updateStatus(@RequestBody EamRepairOrderStatusDTO eamRepairOrderDTO) {
        eamRepairOrderService.updateStatus(eamRepairOrderDTO);
        return RetResult.ok();
    }

    /**
     * 重新维修
     */
    @ApiOperation("重新维修")
    @PostMapping("/againRepair")
    @RequestLogHeader(modular = "维修单管理", businessType = "重新维修")
    public RetResult<?> againRepair(@RequestBody EamRepairOrderStatusDTO eamRepairOrderDTO) {
        EamRepairOrder repairOrder = eamRepairOrderService.getById(eamRepairOrderDTO.getId());
        Assert.isTrue(repairOrder != null, "维修单不存在");
        Assert.isTrue(repairOrder.getRepairStatus().equals("3"), "维修单未维修完成或已确认");
        repairOrder.setRepairStartTime(new Date());
        repairOrder.setRepairStatus("2");
        repairOrder.setMemo(eamRepairOrderDTO.getMemo());
        eamRepairOrderService.setDefaultValue(repairOrder);
        eamRepairOrderService.updateById(repairOrder);
        eamWorkOrderRecordService.saveRepairRecord(repairOrder, RepairOrderRecordOperateType.RE_REPAIR, eamRepairOrderDTO.getMemo());
        return RetResult.ok();
    }


    /**
     * 委外开始
     */
    @ApiOperation("委外开始")
    @PostMapping("/start/outSourcing")
    @RequestLogHeader(modular = "维修单管理", businessType = "删除")
    public RetResult<?> outSourcingStart(@RequestBody EamRepairOrderStatusDTO eamRepairOrderDTO) {
        eamRepairOrderService.outSourcingStart(eamRepairOrderDTO);
        return RetResult.ok();
    }



    /**
     * 委外完成
     */
    @ApiOperation("委外完成")
    @PostMapping("/finish/outSourcing")
    @RequestLogHeader(modular = "维修单管理", businessType = "删除")
    public RetResult<?> outSourcingFinish(@RequestBody EamRepairOrderStatusDTO eamRepairOrderDTO) {
        eamRepairOrderService.outSourcingFinish(eamRepairOrderDTO);
        return RetResult.ok();
    }

    /**
     * 转交维修单
     */
    @ApiOperation("转交")
    @PostMapping("/transmit")
    @RequestLogHeader(modular = "维修单管理", businessType = "删除")
    public RetResult<?> transmit(@RequestBody EamRepairOrderTransmitDTO eamRepairOrderTransmitDTO) {
        eamRepairOrderService.transmit(eamRepairOrderTransmitDTO);

        return RetResult.ok();
    }


    /**
     * 指派维修单
     */
    @ApiOperation("指派")
    @PostMapping("/assign")
    @RequestLogHeader(modular = "维修单管理", businessType = "删除")
    public RetResult<?> assign(@RequestBody EamRepairOrderStatusDTO eamRepairOrderDTO) {
        eamRepairOrderService.assign(eamRepairOrderDTO);
        return RetResult.ok();
    }

}
